'use strict';
const dayjs = require("dayjs");

module.exports = app => {
  const { INTEGER, DATE } = app.Sequelize;
  
  const RolePermissions = app.model.define(
    'RolePermissions',
    {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      role_id: { type: INTEGER, allowNull: false },
      permission_id: { type: INTEGER, allowNull: false },
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      },
      update_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      }
    },
    {
      tableName: 'role_permissions',
      timestamps: true,
      createdAt: 'create_time',
      updatedAt: 'update_time',
      indexes: [
        {
          unique: true,
          fields: ['role_id', 'permission_id']
        }
      ]
    }
  );

  return RolePermissions;
};
